package com.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Discription:
 */
public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取 web.xml 中配置的上下文参数 context-param
        ServletContext context = getServletConfig().getServletContext();
        String username = context.getInitParameter("username");
        String password = context.getInitParameter("password");
        System.out.println("context-param参数的username的值是："+username);
        System.out.println("context-param参数的password的值是："+password);
        //这里不能获取init-param里的内容，只能由servletConfig获取
        //而context-param只能由servletContext对象获取的

        //2、获取当前的工程路径，格式: /工程路径
        System.out.println("当前工程路径："+context.getContextPath());

        //3、获取工程部署后在服务器硬盘上的绝对路径
        System.out.println("工程部署的路径是："+context.getRealPath("/"));
        /*
            /被服务器解析地址为：http:/ip:port/工程名/
            context.getRealPath("/")获取的是部署的工程的地址
            映射到IDEA代码的web目录，即是当前工程的web的目录下
         */
        //可以获取当前工程下的css目录的绝对路径
        System.out.println("当前工程下的css目录绝对路径："+context.getRealPath("/css"));
    }
}
